<template>
  <el-dialog title="新增违章"
             :visible.sync="detailShow"
             :before-close="handleClose"
             width="700px"
             :close-on-click-modal="false">
    <el-form :label-position="labelPosition"
             class="violation"
             ref="form"
             :model="queryParams"
             :rules="rules"
             label-width="120px">
      <el-form-item label="选择寝室"
                    prop="roomId">
        <el-select v-model="queryParams.schoolAreaId"
                   placeholder="--校区--"
                   @change="cascade('campus')"
                   size="small">
          <el-option v-for="(item, index) in campus"
                     :key="index"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
        <el-select v-model="queryParams.campusId"
                   placeholder="--园区--"
                   @change="cascade('gardens')"
                   size="small">
          <el-option v-for="(item, index) in gardens"
                     :key="index"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
        <el-select v-model="queryParams.buildingId"
                   placeholder="--楼栋--"
                   @change="cascade('building')"
                   size="small">
          <el-option v-for="(item, index) in buildings"
                     :key="index"
                     :label="item.number + '栋' + item.unitNumber + '单元'"
                     :value="item.id">
          </el-option>
        </el-select>
        <el-select v-model="queryParams.floorId"
                   placeholder="--楼层--"
                   @change="cascade('floor')"
                   size="small">
          <el-option v-for="(item, index) in floors"
                     :key="index"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
        <el-select v-model="queryParams.roomId"
                   placeholder="--房间--"
                   @change="cascade('room')"
                   size="small">
          <el-option v-for="(item, index) in rooms"
                     :key="index"
                     :label="item.name"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检查日期"
                    prop="checkDate">
        <el-date-picker v-model="queryParams.checkDate"
                        size="small"
                        style="width: 215px;"
                        format="yyyy-MM-dd"
                        value-format="timestamp"
                        type="date"
                        placeholder="选择检查日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="违章内容"
                    prop="content">
        <el-input type="textarea"
                  v-model="queryParams.content"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="preClose"
                 size="small">取 消</el-button>
      <el-button type="primary"
                 size="small"
                 :loading="saveLoading"
                 @click="handleConfirm">确认</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { listRoomByCollegeAndFloorUsingGET, listFloorByBuildingIdUsingGET, listBuildingByCampusIdAndUserUsingGET, listCampusBySchoolAreaIdAndUserUsingGET, listSchoolAreaByUserUsingGET, addViolationUsingPOST } from '@api/auto'
export default {
  name: '',
  props: {
    detailShow: Boolean,
    handleClose: Function
  },
  mixins: [],
  components: {},
  data () {
    return {
      saveLoading: false,
      labelPosition: 'top',
      // 校区
      campus: [],
      // 园区
      gardens: [],
      // 楼栋
      buildings: [],
      // 楼层
      floors: [],
      // 房间
      rooms: [],
      queryParams: {
        checkDate: new Date(),
        schoolAreaId: '',
        campusId: '',
        buildingId: '',
        floorId: '',
        roomId: '',
        checkWorkId: '',
        content: ''
      },
      rules: {
        roomId: [
          { required: true, message: '请选择寝室', trigger: 'blur' }
        ],
        checkDate: [
          { required: true, message: '请选择检查日期', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入违章内容', trigger: 'blur' },
          { min: 1, max: 200, message: '长度在1到200个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
  },
  mounted () {
    this.listSchoolAreaByUserUsingGET()
  },
  methods: {
    handleConfirm () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveLoading = true
          addViolationUsingPOST(this.queryParams)
            .then(() => {
              this.$message.success('新增成功！')
              this.saveLoading = false
              this.preClose()
            })
            .catch(() => {
              this.saveLoading = false
            })
        }
      })
    },
    init () {
      this.gardens = []
      this.buildings = []
      this.floors = []
      this.rooms = []
      this.queryParams = {
        checkDate: new Date(),
        schoolAreaId: '',
        campusId: '',
        buildingId: '',
        floorId: '',
        roomId: '',
        checkWorkId: '',
        content: ''
      }
    },
    preClose () {
      this.init()
      this.$refs.form.resetFields()
      this.handleClose()
    },
    cascade (type) {
      switch (type) {
        case 'campus':
          this.listCampusBySchoolAreaIdAndUserUsingGET()
          break
        case 'gardens':
          this.listBuildingByCampusIdAndUserUsingGET()
          break
        case 'building':
          this.listFloorByBuildingIdUsingGET()
          break
        case 'floor':
          this.listRoomByCollegeAndFloorUsingGET()
          break
        case 'room':
          this.changeRoom()
      }
    },
    // 用户根据登陆用户查询可以查看的校区
    listSchoolAreaByUserUsingGET () {
      listSchoolAreaByUserUsingGET()
        .then(res => {
          this.campus = res.data
        })
    },
    // 根据登陆用户查询指定校区下的园区
    listCampusBySchoolAreaIdAndUserUsingGET () {
      this.queryParams.campusId = null
      this.queryParams.buildingId = null
      this.queryParams.floorId = null
      this.queryParams.roomId = null
      this.buildings = []
      this.gardens = []
      this.floors = []
      this.rooms = []
      listCampusBySchoolAreaIdAndUserUsingGET(this.queryParams.schoolAreaId)
        .then(res => {
          this.gardens = res.data
        })
    },
    // 根据园区id和登陆用户查询管辖的楼栋
    listBuildingByCampusIdAndUserUsingGET () {
      this.queryParams.buildingId = null
      this.queryParams.floorId = null
      this.queryParams.roomId = null
      this.buildings = []
      this.floors = []
      this.rooms = []
      listBuildingByCampusIdAndUserUsingGET(this.queryParams.campusId)
        .then(res => {
          this.buildings = res.data
        })
    },
    // 根据楼栋查楼层
    listFloorByBuildingIdUsingGET () {
      this.queryParams.floorId = null
      this.queryParams.roomId = null
      this.floors = []
      this.rooms = []
      listFloorByBuildingIdUsingGET(this.queryParams.buildingId)
        .then(res => {
          this.floors = res.data
        })
    },
    // 根据楼层查房间
    listRoomByCollegeAndFloorUsingGET () {
      this.queryParams.roomId = null
      this.rooms = []
      listRoomByCollegeAndFloorUsingGET(this.queryParams.floorId, -1)
        .then(res => {
          this.rooms = res.data
        })
    }
  }
}
</script>
<style lang='less' scoped>
</style>
<style lang="less">
.violation .el-form-item__label {
  width: 80px;
}
</style>
